package com.itheima.controller.backend.security;

import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckGroupService;
import com.itheima.service.CheckItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/checkgroup")
@Slf4j
public class CheckGroupController {

    @Autowired
    private CheckGroupService checkGroupService;

    @Autowired
    private CheckItemService checkItemService;

    /**
     * 新增检查组
     * @param checkGroup
     * @param checkitemIds
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
        log.info("添加检查组和检查项：{},{}",checkGroup,checkitemIds);
        try {
            checkGroupService.add(checkGroup,checkitemIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"添加失败");
        }
        return new Result(true,"添加成功");
    }

    /**
     * 分页查询
     *
     */
    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = checkGroupService.findPage(queryPageBean);
        return pageResult;
    }

    //根据id删除检查组
    @GetMapping("/deleteById")
    public Result deleteById(@RequestParam Integer id){
        log.info("根据id删除检查组：{}",id);
        checkGroupService.deleteById(id);
        return new Result<>(true,"删除检查组成功");
    }

    //根据id查询检查组
    @GetMapping("/findById")
    public Result<CheckGroup> findById(@RequestParam Integer id){
        log.info("根据id获取检查组信息：{}",id);
        CheckGroup checkGroup = checkGroupService.findById(id);
        return new Result(true,"查询成功",checkGroup);
    }

    //根据检查组id查询检查组id包含的多个检查项id
    @GetMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(@RequestBody Integer id){
        try {
            List<Integer> checkItemIds = checkGroupService.findCheckItemIdsByCheckGroupId(id);
            return new Result(true,"查询成功",checkItemIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询失败");

    }}




        /**
         * 编辑检查组
         * @param checkGroup
         * @param checkitemIds
         * @return
         */
        @PostMapping("/edit")
        public Result edit(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
            log.info("修改检查组和检查项：{},{}",checkGroup,checkitemIds);
            try {
                checkGroupService.edit(checkGroup,checkitemIds);
            } catch (Exception e) {
                e.printStackTrace();
                return new Result(false,"修改失败");
            }
            return new Result(true,"修改成功");
        }

    /**
     * 查询所有的检查组(添加套餐中的检查组)
     */
    @GetMapping("/findAll")
    public Result findAll(){
        try {
            List<CheckGroup> lists = checkGroupService.findAll();
            return new Result(true,"查询检查项成功",lists);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询检查项失败");
        }
    }
}




























